﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
using JumbotCms.Common;
namespace JumbotCms.WebFile.Admin
{
    public partial class _channeledit0 : JumbotCms.API.AdminCenter
    {
        public string cType = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            ChannelId = Str2Str(q("ccid"));
            id = Str2Str(q("id"));
            Admin_Load("000000", "html", false);
            bool isModules = true;
            bool isTemplate = true;
            if (ChannelId == "0")
                cType = q("cType");
            else
                cType = ChannelType;
            if (!Page.IsPostBack)
            {
                doh.Reset();
                doh.SqlCmd = "SELECT ID,Title FROM [jcms_normal_modules] WHERE [Enabled]=1 AND [Type]='" + cType + "' ORDER BY pId";
                DataTable dtModules = doh.GetDataTable();
                if (dtModules.Rows.Count < 1)
                    isModules = false;
                dtModules.Clear();
                dtModules.Dispose();
                if (!isModules)
                {
                    FinalMessage("指定模块不存在或被禁用", site.Dir + "admin/close.htm", 0);
                    btnSave.Enabled = false;
                }
                doh.Reset();
                doh.SqlCmd = "SELECT ID,Title FROM [jcms_normal_template] WHERE [Type]='" + cType + "' and [sType]='Channel' ORDER BY IsDefault desc,id asc";
                DataTable dtTemplate = doh.GetDataTable();
                if (dtTemplate.Rows.Count < 1)
                    isTemplate = false;
                this.ddlTemplate.DataSource = dtTemplate;
                this.ddlTemplate.DataTextField = "Title";
                this.ddlTemplate.DataValueField = "ID";
                this.ddlTemplate.DataBind();
                dtTemplate.Clear();
                dtTemplate.Dispose();
                doh.Reset();
                doh.SqlCmd = "SELECT ID,Title FROM [jcms_normal_thumbs] ORDER BY ChannelId desc,id asc";
                DataTable dtThumbs = doh.GetDataTable();
                this.ddlDefaultThumbs.DataSource = dtThumbs;
                this.ddlDefaultThumbs.DataTextField = "Title";
                this.ddlDefaultThumbs.DataValueField = "ID";
                this.ddlDefaultThumbs.DataBind();
                dtThumbs.Clear();
                dtThumbs.Dispose();
            }
            if (!isTemplate)
            {
                FinalMessage("未找到模板，请先添加模板", site.Dir + "admin/close.htm", 0);
                btnSave.Enabled = false;
            }
            this.txtEditor1.BasePath = site.Dir + "editors/114editor/";
            Jumbot.DataOper.WebFormHandler wh = new Jumbot.DataOper.WebFormHandler(doh, "jcms_normal_channel", btnSave);
            wh.AddBind(txtTitle, "Title", true);
            wh.AddBind(txtEditor1, "Value", "Info", true);
            wh.AddBind(ddlClassDepth, "ClassDepth", false);
            wh.AddBind(txtDir, "Dir", true);
            wh.AddBind(txtItemName, "ItemName", true);
            wh.AddBind(txtItemUnit, "ItemUnit", true);
            wh.AddBind(rblIsHtml, "SelectedValue", "IsHtml", false);
            wh.AddBind(rblTarget, "SelectedValue", "Target", false);
            wh.AddBind(rblEnabled, "SelectedValue", "Enabled", false);
            wh.AddBind(ddlDefaultThumbs, "DefaultThumbs", false);
            wh.AddBind(rblIsPost, "SelectedValue", "IsPost", false);
            wh.AddBind(ddlTemplate, "TemplateId", false);
            if (ChannelId != "0")
            {
                wh.ConditionExpress = "id=" + ChannelId;
                wh.Mode = Jumbot.DataOper.OperationType.Modify;
                this.txtDir.Enabled = false;
                this.ddlClassDepth.Enabled = false;
            }
            else
            {
                wh.Mode = Jumbot.DataOper.OperationType.Add;
                this.rblIsHtml.SelectedValue = "0";
            }
            if (!site.IsHtml) this.rblIsHtml.Enabled = false;
            wh.validator = chkForm;
            wh.BindBeforeModifyOk += new EventHandler(bind_ok);
            wh.AddOk += new EventHandler(add_ok);
            wh.ModifyOk += new EventHandler(save_ok);
        }
        protected void bind_ok(object sender, EventArgs e)
        {
        }
        protected bool chkForm()
        {
            if (!CheckFormUrl())
                return false;
            if (!Page.IsValid)
                return false;
            doh.Reset();
            doh.ConditionExpress = "Title=@title and id<>" + ChannelId;
            doh.AddConditionParameter("@title", txtTitle.Text);
            if (doh.Exist("jcms_normal_channel"))
            {
                FinalMessage("频道名重复!", "", 1);
                return false;
            }
            doh.Reset();
            if (ChannelId == "0") //新增加
            {
                doh.ConditionExpress = "dir=@dir";
                if (System.IO.Directory.Exists(Server.MapPath(site.Dir) + txtDir.Text))
                {
                    FinalMessage("名为" + txtDir.Text + "的文件夹已存在!", "", 1);
                    return false;
                }
            }
            else
            {
                doh.ConditionExpress = "dir=@dir and id<>" + ChannelId;
            }
            doh.AddConditionParameter("@dir", txtDir.Text);
            if (doh.Exist("jcms_normal_channel"))
            {
                FinalMessage("目录名重复!", "", 1);
                return false;
            }
            this.txtDir.Text = this.txtDir.Text.ToLower();
            return true;
        }
        protected void add_ok(object sender, EventArgs e)
        {
            doh.Reset();
            doh.ConditionExpress = "id>0 ORDER BY pId desc";
            int pId = Jumbot.Common.Validator.StrToInt(doh.GetValue("jcms_normal_channel", "pId").ToString(), 0);
            doh.Reset();
            doh.ConditionExpress = "Title=@title";
            doh.AddConditionParameter("@title", txtTitle.Text);
            doh.AddFieldItem("pId", pId + 1);
            doh.AddFieldItem("IsOut", 0);
            doh.AddFieldItem("IsNav", 0);
            doh.AddFieldItem("Type", cType);
            doh.Update("jcms_normal_channel");
            FinalMessage("频道成功保存,请执行“生成目录”", site.Dir + "admin/close.htm", 0);
        }
        protected void save_ok(object sender, EventArgs e)
        {
            FinalMessage("频道成功保存,请执行“生成目录”", site.Dir + "admin/close.htm", 0);
        }
    }
}
